package com.wenqiner.requesttool.view;

import cn.hutool.core.text.CharSequenceUtil;
import com.wenqiner.requesttool.core.exception.CustomException;
import com.wenqiner.requesttool.core.fx.AlertTool;
import com.wenqiner.requesttool.domain.HttpResponseData;
import com.wenqiner.requesttool.util.CommonUtil;
import com.wenqiner.requesttool.util.TableUtil;
import com.wenqiner.requesttool.util.HttpUtil;
import com.wenqiner.requesttool.util.ParsePlaceholderUtil;
import com.wenqiner.requesttool.view.base.AbstractRequestLayout;
import com.wenqiner.requesttool.view.base.CommonController;
import com.wenqiner.requesttool.view.base.InnerParamProcess;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

/**
 * 常规请求controller
 *
 * @author rhf
 * @date 2023-02-28
 **/
public class GrooveRequestController extends AbstractRequestLayout implements CommonController, InnerParamProcess {
    public static final Logger log = LoggerFactory.getLogger(GrooveRequestController.class);

    @FXML
    protected GridPane paramTable;
    @FXML
    protected GridPane headerTable;

    @Override
    public void init() {
    }

    @FXML
    protected void addParamRowsEvent(ActionEvent event) throws IOException {
        super.createTable(paramTable, super.gridPaneNextRowNum(paramTable));
    }

    @FXML
    protected void addHeaderRowsEvent(ActionEvent event) throws IOException {
        super.createTable(headerTable, super.gridPaneNextRowNum(headerTable));
    }

    @FXML
    protected TextArea requestOriginalData;
    @FXML
    protected TextArea responseOriginalData;
    @FXML
    protected ComboBox<String> protocolType;
    @FXML
    protected TextField requestUrl;

    @FXML
    protected void sendRequest(ActionEvent event) throws IOException {
        //获取全部系统参数
        Map<String, String> paramMap = TableUtil.parseHeader(paramTable);
        //获取全部header
        Map<String, String> headerMap = TableUtil.parseHeader(headerTable);
        //获取地址
        String requestPath = String.join("", protocolType.getValue(), requestUrl.getText());
        //获取请求参数
        String text = requestOriginalData.getText();
        if (CharSequenceUtil.isBlank(text)) {
            AlertTool.showError(" 请输入请求报文");
            return;
        }

        headerMap.keySet().forEach(key -> {
            try {
                headerMap.put(key, ParsePlaceholderUtil.parseMethodOuter(headerMap.get(key)));
            } catch (Exception e) {
            }
        });
        HttpResponseData httpResponseData;
        try {
            httpResponseData = HttpUtil.doPost(requestPath, headerMap, ParsePlaceholderUtil.parseMethodOuter(text));
        } catch (CustomException e) {
            AlertTool.showError(e.getMsg());
            return;
        }catch (Exception e)
        {
            AlertTool.showError("请求异常");
            log.info("发送请求异常,异常信息:[{}],e:{}",e.getMessage(),e);
            return;
        }
        responseOriginalData.setText(CommonUtil.formatJson(httpResponseData.getData()));
    }


    @FXML
    protected void paramFormatEvent(ActionEvent event) {
        String oriReqStr = requestOriginalData.getText();
        requestOriginalData.setText(CommonUtil.formatJson(oriReqStr));
        String oriResStr = responseOriginalData.getText();
        responseOriginalData.setText(CommonUtil.formatJson(oriResStr));
    }
}
